package org.xiaoxin.onecoupon.engine.mq.producer;

import cn.hutool.core.util.StrUtil;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.xiaoxin.onecoupon.engine.mq.base.BaseSendExtendDTO;
import org.xiaoxin.onecoupon.engine.mq.base.MessageWrapper;
import org.xiaoxin.onecoupon.engine.mq.event.UserCouponRedeemEvent;

import java.util.UUID;

/**
 * 用户兑换优惠券消息生产者
 * @author xiaoxin
 * 2025/5/13
 */
@Component
public class UserConponRedeemProducer extends  AbstractCommonSendProduceTemplate<UserCouponRedeemEvent>{
   private final ConfigurableEnvironment environment;

   public UserConponRedeemProducer(@Autowired RocketMQTemplate rocketMQTemplate, @Autowired ConfigurableEnvironment environment) {
      super(rocketMQTemplate);
      this.environment = environment;
   }
   @Override
   protected BaseSendExtendDTO buildBaseSendExtendParam(UserCouponRedeemEvent messageSendEvent) {
      return BaseSendExtendDTO.builder()
              .eventName("用户兑换优惠券")
              .keys(UUID.randomUUID().toString())
              .topic(environment.resolvePlaceholders("one-coupon_engine-service_coupon-redeem_topic${unique-name:}"))
              .sentTimeout(2000L)
              .build();
   }

   @Override
   protected Message<?> buildMessage(UserCouponRedeemEvent messageSendEvent, BaseSendExtendDTO requestParam) {
      String keys = StrUtil.isEmpty(requestParam.getKeys()) ? UUID.randomUUID().toString() : requestParam.getKeys();
      return MessageBuilder
              .withPayload(new MessageWrapper(requestParam.getKeys(), messageSendEvent))
              .setHeader(MessageConst.PROPERTY_KEYS, keys)
              .setHeader(MessageConst.PROPERTY_TAGS, requestParam.getTag())
              .build();
   }
}
